﻿using System;
using System.ComponentModel;
using MECPSDB.EntityClasses;
using MECPSDB.FactoryClasses;
using MECPSDB.HelperClasses;
using MECUITMPLTLIB;
using MECPSDB.DatabaseSpecific;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace MEC.WMS.FileMaintenance.MatGroup {
    
    public partial class AeFrm:AeFrmUiTemplate{
        
        private const string Caption = "Material Group  Maintenance";
        
        public AeFrm(string cs, string branchcode, string usercode){
            if (cs == null){
                throw new ArgumentNullException("cs");
            }
            Cs= cs;
            Branchcode = branchcode;
            Usercode = usercode;
            InitializeComponent();
            this.Text = Caption;
        }

        public override sealed string Text{
            get{
                return base.Text;
            }
            set{
                base.Text = value;
            }
        }

        public override void MethodNewEntityProcedure(){
           base.MethodNewEntityProcedure();
           //todone set default for the newly added entity by using another object
           MatgroupEntity entity = new MatgroupEntity(); //-implementation
           //todone Add extra code here
           //... 
           //todone assign the new entity to the entity object bounded to the form 
           MainEntity = entity; //-implementation
           //todone must put at the end when there is no error - allow edit to true
           MethodAllowEdit(true); ////-implementation
       }


        protected override void AeFrmUiTemplateLoad(object sender, EventArgs e){
        //implements generic setting of Dexex controls behavior
            base.AeFrmUiTemplateLoad(sender,e); 
        //todo - done
        }


        protected override void MainEntityBackgroundSaveWorkCompleted(object sender, RunWorkerCompletedEventArgs e){
            base.MainEntityBackgroundSaveWorkCompleted(sender,e);
        //todo - done
        }

        protected override void MainEntityBackgroundSaveDoWork(object sender, DoWorkEventArgs e){
            MatgroupEntity entity = (MatgroupEntity) e.Argument;
            using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(Cs)){

                if (entity.IsNew){} else{}

                dataAccessAdapter.SaveEntity(entity, true);
                e.Result = entity;
            }

        }

        protected override void MethodLoadControlValuesToObjectsForSaving(){
        //todo -done
            MatgroupEntity entity = (MatgroupEntity) MainEntity;
            entity.MatGrpCode = (string) textEditGroupCode_ae_pk_.EditValue;
            entity.MatGrpDescription = (string) textEditDescription_ae_.EditValue;
            entity.Remarks = (string) memoEditRemarks_ae_.EditValue;
            }

        protected override bool MethodValidate(){
            dxErrorProvider1.ClearErrors();
            bool focused = false;
            if (string.IsNullOrWhiteSpace(textEditGroupCode_ae_pk_.Text)){
                dxErrorProvider1.SetError(textEditGroupCode_ae_pk_, "Required");
                textEditGroupCode_ae_pk_.Focus();
                focused = true;
            }
            if (string.IsNullOrWhiteSpace(textEditDescription_ae_.Text)){
                dxErrorProvider1.SetError(textEditDescription_ae_, "Required");
                if (!focused){
                    textEditDescription_ae_.Focus();
// ReSharper disable RedundantAssignment
                    focused = true;
// ReSharper restore RedundantAssignment
                }
            }
            
            return !dxErrorProvider1.HasErrors;
        }

        protected override void MethodClearForm(){
        //todo - done not needed
            base.MethodClearForm();
            //todo extra handling
            //checkEditInActive_ae_.Checked = false;
            //checkEditWdSerial_ae_.Checked = false;
        }
        protected override void MethodDispayMainEntityToControls(){
            base.MethodDispayMainEntityToControls();
        //todo - done
            MatgroupEntity entity = (MatgroupEntity) MainEntity;
            textEditGroupCode_ae_pk_.EditValue = entity.MatGrpCode;
            textEditDescription_ae_.EditValue = entity.MatGrpDescription;
            memoEditRemarks_ae_.EditValue = entity.Remarks;
       }

        protected override void MainEntityFetchProgressChanged(object sender, ProgressChangedEventArgs e){
            base.MainEntityFetchProgressChanged(sender, e);
            //todo -done = not needed
            //EntityCollection ec = (EntityCollection) e.UserState;
            //if (ecbrand.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
            //    ecbrand = ec;
            //    lookUpEditBrandCode_ae_.Properties.DataSource = ec;
            //}else
            //if (ecgroup.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
            //    ecgroup = ec;
            //    lookUpEditGroupCode_ae_.Properties.DataSource = ec;
            //}else
            //if (ecuom.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
            //    ecuom = ec;
            //    lookUpEditUomCode_ae_.Properties.DataSource = ec;
            //}           

        }
        protected override void MainEntityBackgroundFetchDoWork(object sender, DoWorkEventArgs e){
            FetchParam fetchParam = (FetchParam) e.Argument;
            //BackgroundWorker bw = (BackgroundWorker) sender;
            //TODOne LookupFetches - not needed
            //TODOne MainEntityFetch
            if (!fetchParam.FetchMainEntity) return;

            MatgroupEntity entity = new MatgroupEntity(fetchParam.PkCode);
            IPrefetchPath2 prefetchPath = null;
            ExcludeIncludeFieldsList excludedfields = new ExcludeIncludeFieldsList();
            using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(fetchParam.Cs)){
                dataAccessAdapter.FetchEntity(entity, prefetchPath, null, excludedfields);
                fetchParam.MainEntityFetched = entity;
                e.Result = fetchParam;
                return;
            }
        }


        
    }//End class
} // End namespace